<html>
<head><meta charset="utf-8"><title>Allocator lifetime · t-libs/wg-allocators · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/index.html">t-libs/wg-allocators</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html">Allocator lifetime</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="186531291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186531291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186531291">(Jan 24 2020 at 20:33)</a>:</h4>
<p>I can't find a mention of lifetimes in the the allocator traits of <code>alloc-wg</code>. However, some allocators may be local in the sense that their own lifetime and that of the allocated memory is limited. For example, a stack allocator or an instance of bumpalo that is not in a static. How is and how should this be handled?</p>



<a name="186537473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186537473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Erich Gubler <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186537473">(Jan 24 2020 at 21:46)</a>:</h4>
<p>The type implementing <code>AllocRef</code> is expected to encapsulate the lifetime if necessary -- so, implementing it for a reference to a <code>bumpalo</code> memory pool is what you would do, not implement it directly on a pool instance. Does that make sense?</p>



<a name="186548936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186548936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186548936">(Jan 25 2020 at 00:42)</a>:</h4>
<p>We rename <code>Alloc</code> to <code>AllocRef</code> because types that implement <code>Alloc</code> are a reference, smart pointer, or ZSTs. This question is another reason, why this rename is a good idea.</p>
<p>It is not possible to have an allocator like <code>MyAlloc([u8; N])</code>, that owns the memory and also implements <code>Alloc</code>. That would mean, that moving a <code>Vec&lt;T, MyAlloc&gt;</code> would need to correct the internal pointer, which is not possible. You have to encapsule the lifetime yourself by <code>impl AllocRef for &amp;(mut) MyAllocator</code>, or pass the lifetime to the allocator directly. The former is prefered IMO.</p>



<a name="186549155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549155">(Jan 25 2020 at 00:47)</a>:</h4>
<p>Would the same "trick" be necessary to make types using not-thread-safe allocation?</p>
<p>In other words, a Vec you <strong>can't</strong> Send because the allocator only lets you dealloc in the same thread that made the allocation in to start?</p>



<a name="186549166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549166">(Jan 25 2020 at 00:47)</a>:</h4>
<p>Or would those take even further type hackery?</p>



<a name="186549219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549219">(Jan 25 2020 at 00:48)</a>:</h4>
<p>I havn't tried this out, but my first guess is, that's possible.</p>



<a name="186549262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549262">(Jan 25 2020 at 00:49)</a>:</h4>
<p>that certainly seems like a very important case to support</p>



<a name="186549292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549292">(Jan 25 2020 at 00:50)</a>:</h4>
<p>eg, the windows process heap is thread safe, so slow, but you can make thread local heaps if you want to go faster</p>



<a name="186549876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549876">(Jan 25 2020 at 01:03)</a>:</h4>
<p>Can't you just implement <code>!Send</code> for your allocator?</p>



<a name="186549891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186549891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186549891">(Jan 25 2020 at 01:03)</a>:</h4>
<p>Something like in <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0633731592517babaab4cc0ad8c6443a" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0633731592517babaab4cc0ad8c6443a">this playground</a>?</p>



<a name="186550594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186550594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186550594">(Jan 25 2020 at 01:19)</a>:</h4>
<p>Probably, I mostly wasn't sure if it'd work out like that with allocref too</p>



<a name="186550996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186550996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186550996">(Jan 25 2020 at 01:28)</a>:</h4>
<p>I just tried it out with <code>impl ... for MyAlloc</code>, <code>impl ... for &amp;MyAlloc</code>, and <code>impl ... for &amp;mut MyAlloc</code>. Everything works :)</p>



<a name="186552379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186552379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186552379">(Jan 25 2020 at 02:05)</a>:</h4>
<p>That is a very interesting way of inheriting properties of the allocator to the allocations.</p>



<a name="186553565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186553565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186553565">(Jan 25 2020 at 02:40)</a>:</h4>
<p>This way is only suited for very few traits, as calling those traits is ugly. We don't expect users to call allocators directly in most cases. Mostly they will be used behind a generic type, which abstracts away this.</p>



<a name="186553979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186553979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186553979">(Jan 25 2020 at 02:54)</a>:</h4>
<p>sounds like a red flag to me</p>



<a name="186553996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186553996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186553996">(Jan 25 2020 at 02:55)</a>:</h4>
<p>directly using the allocator shouldn't give the user a hard time.</p>



<a name="186554412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186554412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186554412">(Jan 25 2020 at 03:08)</a>:</h4>
<p>To me as well, Box was being specified to be equivalent to allocating the layout exactly so that one can manually allocate and initialize it for example.</p>



<a name="186569963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186569963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tim Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186569963">(Jan 25 2020 at 11:38)</a>:</h4>
<p>I have tried out this a bit more. When implementing <code>AllocRef</code> on <code>MyAlloc</code>, everything works as before. When implementing it on <code>&amp;MyAlloc</code>, the methods has to be called on a reference. This means either, you need </p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">my_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">my_alloc</span><span class="p">;</span><span class="w"></span>
<span class="n">my_ref</span><span class="p">.</span><span class="n">alloc</span><span class="p">(...);</span><span class="w"></span>
</pre></div>


<p>or</p>
<div class="codehilite"><pre><span></span><span class="p">(</span><span class="o">&amp;</span><span class="n">my_alloc</span><span class="p">).</span><span class="n">alloc</span><span class="p">(...);</span><span class="w"></span>
</pre></div>



<a name="186810005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186810005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186810005">(Jan 28 2020 at 18:12)</a>:</h4>
<p>I'm slightly confused what it means to implement <code>AllocRef</code> for a <code>&amp;mut MyAllocator</code>. How long would the returned memory need to be valid and can be assumed unique? Is it the lifetime of the <code>BuildAllocRef</code> that returned this mutable reference? Or the lifetime of the mutable reference itself? That is important if I have a bump allocator with reset.</p>



<a name="186810636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/197181-t-libs/wg-allocators/topic/Allocator%20lifetime/near/186810636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/197181-t-libs/wg-allocators/topic/Allocator.20lifetime.html#186810636">(Jan 28 2020 at 18:18)</a>:</h4>
<p>I'm asking about the mutable reference in particular since the <code>BuildAllocRef</code> can not return the original reference with its full lifetime even if <code>Self = &amp;mut MyAllocator</code>. For a <code>BuildAllocRef</code> that returns <code>&amp;_</code> this is possible by cloning some original reference which it contains or is.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>